package com.tevintao.marketingsystem.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWebView;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.Random;

import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByClassName;


/**
 * 通过搜索添加好友，接受好友
 */
public class AddSubscribeProcess extends SetTaskExtra {
    private static AddSubscribeProcess ourInstance = new AddSubscribeProcess();

    public static AddSubscribeProcess getInstance() {
        return ourInstance;
    }

    private AddSubscribeProcess() {
    }

    private Random mRandom = new Random();

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
//        WxUiViewConfig.printViewGroup((ViewGroup) v);
        if (WxUiViewConfig.VIEW_SEARCH_SUBSCRIBE_LIST_WEB_VIEW.equals(contextName)) {
            View editView = getViewByClassName((ViewGroup) v, "ActionBarEditText");
            if (null != editView && editView.getVisibility() == View.VISIBLE && editView instanceof EditText) {
                EditText textview = (EditText) editView;
                if ("".equals(textview.getText().toString())) {
                    //TextView textView = (TextView) buttonViewFaSong;
                    LogManager.getInstance().logInfo("等待自动输入要关注的公众号类型");
                    String[] str = new String[]{
                            "cctvnewscenter", "xlmw99", "duhaoshu", "QQ_shijuezhi", "vipnzd", "youshucc", "shiguangxiangji", "mweishijie", "ge11345", "yinyue2016",
                            "qinghuananbu", "guoxue33", "yuedu58", "yinyue068", "cctvyscj", "xhrb2013", "safe01", "jyxlx888", "gsh688", "gxsh789",
                            "gxsh369", "zaoanznl", "jcyl8888", "gxjhshys", "wsc005", "gxjcwz", "qnwzwx", "jkys365", "lingdianys", "mryd100",
                            "cyxinyuan", "dzwz999", "gxzh110", "mmys9999", "people_rmw", "zhiqing360", "harvard8888", "jk13872050678", "meiwengx", "nengliang2012",
                            "iiiread", "mms_wcj", "whoissing", "maizi8090", "yilushuxiang", "zhrs888888", "heart3000", "ygandong", "yuedu365", "jiajiaozh",
                            "geshwx", "wishbooks", "rszx188", "foxue688", "yilinzazhi", "yilinxiaowenxue", "yilinshaonianban", "lzwy111", "lizhi743", "jd6696",
                            "yichan360", "zvip12", "xinggaizs", "weixinzazhi", "vvxixi", "ifeng_news", "gudianshucheng", "duzhewenxue", "weixinqing361", "mrjc888",
                            "vzhihui", "dgg43210", "foyulu", "fengshui800", "lxyymw", "bjdzg80", "chanchayiwei", "yijuhuaxinqing", "amy6562", "hebtvzhhsc",
                            "caifu1sheng", "eyes_insight", "lionscn", "vipp202", "lingdianwt", "fsyys8888", "yinyue0101", "jpg202020", "fumubidu01", "viplvxing",
                            "lvxing183", "zhctwh", "vipcgzh", "vip897897", "kd4646", "fushidp", "csduwu", "catxiu", "qiaomen388", "windland01"
                    };
                    ViewOperation.setText(textview, str[mRandom.nextInt(100)]);
                } else {
//                    WxUiViewConfig.printViewGroup((ViewGroup) v);
                    inputKeyEnter(v.getContext());
                }
            }


            View parentView = v.findViewById(WxUiViewConfig.KEY_WEB_VIEW_PARENT);
            String rectStr = null;
            if (WxTaskProcess.DEBUG_MODEL) {
                v.getContext().sendBroadcast(new Intent(HookWebView.ACTION_GET_ELEMENT_RECT));
                Object rectObj = getExtra(HookWebView.KEY_GET_ELEMENT_RECT);
                if (null != rectObj) {
                    LogManager.getInstance().logInfo("rectObj = " + rectObj.toString());
                    rectStr = rectObj.toString();
                }
            } else {
                rectStr = HookWebView.mLastRect;
                LogManager.getInstance().logInfo("lastRect = " + HookWebView.mLastRect);
            }

            if (!TextUtils.isEmpty(rectStr)) {
                String[] rectArray = rectStr.split(",");
                try {
                    if (rectArray.length == 6) {
                        int[] rectInt = new int[rectArray.length];
                        for (int i = 0, len = rectArray.length; i < len; i++) {
                            rectInt[i] = Integer.parseInt(rectArray[i]);
                        }
                        //liRect.top + ',' + liRect.left + ',' + liRect.right + ',' + liRect.bottom + ',' + liRect.width + ',' + liRect.height
                        int clickX = rectInt[1] + rectInt[4] / 2;//left + width/2
                        int clickY = rectInt[0] + rectInt[5] / 2;//top + height/2
                        if (null != parentView) {
                            boolean clickSuccess = ViewOperation.clickViewOffset(parentView, clickX, clickY);
                            LogManager.getInstance().logInfo("clickSuccess = " + clickSuccess);
                        }
                        LogManager.getInstance().logInfo("clickX = " + clickX + " clickY = " + clickY);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    LogManager.getInstance().logError(e.toString());
                }
            }


            if (WxTaskProcess.DEBUG_MODEL) {
                Object jsObj = getExtra(WxUiViewConfig.KEY_FORWARD_SUBSCRIBE_JAVA_SCRIBE);
                LogManager.getInstance().logInfo("jsObj = " + jsObj);
                if (null == jsObj) {
                    LogManager.getInstance().logError("js代码为空");
                    return true;
                }
                Intent intent = new Intent(HookWebView.ACTION_DO_JAVA_SCRIPT);
                Bundle bundle = new Bundle();
                bundle.putString(HookWebView.KEY_DO_JAVA_SCRIPT, jsObj.toString());
                intent.putExtras(bundle);
                v.getContext().sendBroadcast(intent);
            } else {
                Object jsObb = WxTaskManger.getInstance().getExtra(WxUiViewConfig.KEY_FORWARD_SUBSCRIBE_JAVA_SCRIBE);
                if (null != jsObb)
                    HookWebView.getInstance().doScript(jsObb.toString());
            }
        } else if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            View moreview = v.findViewById(2);
            if (null != moreview) {
                ViewOperation.clickView(moreview);
            }

            View addfriend = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, 2131624112, "添加朋友");
            if (null != addfriend) {
                ViewOperation.clickView(addfriend);
            }
            //WxUiViewConfig.clickSearchButton((ViewGroup) v);
        } else if ("com.tencent.mm.plugin.subapp.ui.pluginapp.AddMoreFriendsUI".equals(contextName)) {
            View subscribeView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, 16908310, "公众号");
            if (null != subscribeView) {
                ViewOperation.clickView(subscribeView);
            }
        } else if ("com.tencent.mm.plugin.webview.ui.tools.fts.FTSBizTeachWebViewUI".equals(contextName)) {
            View searchview = v.findViewById(2131625817);
            if (null != searchview) {
                ViewOperation.clickView(searchview);
            }
        } else if ("com.tencent.mm.plugin.profile.ui.ContactInfoUI".equals(contextName)) {
            LogManager.getInstance().logInfo("当前位于  公众号介绍页面 ");
            View followView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, 16908310, "关注");
            if (null != followView) {
                ViewOperation.clickView(followView);
            }
        } else if ("com.tencent.mm.ui.chatting.ChattingUI".equals(contextName)) {
            LogManager.getInstance().logInfo("当前位于     公众号关注成功      后跳转到的与页面");
            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "关注成功");
        }
        return false;
    }

    private void inputKeyEnter(Context context) {
        Intent intent = new Intent(TaskLogicService.ACTION_KEY_RECEIVER);
        Bundle bundle = new Bundle();
        bundle.putInt(TaskLogicService.KEY_INPUT_KEY, TaskLogicService.INPUT_TYPE_ENTER);
        intent.putExtras(bundle);
        context.sendBroadcast(intent);
    }
}
